<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="600" destroy-on-close @closed="$emit('closed')" top="5vh">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="登录账号" prop="username">
				<el-input v-model="form.username" placeholder="用于登录系统" clearable></el-input>
			</el-form-item>
			<template v-if="mode=='add'">
				<el-form-item label="登录密码" prop="password">
					<el-input type="password" v-model="form.password" placeholder="请输入密码" clearable show-password></el-input>
				</el-form-item>
			</template>
			<el-form-item label="昵称" prop="nickName">
				<el-input v-model="form.nickName" placeholder="请输入昵称" clearable></el-input>
			</el-form-item>
			<el-form-item label="性别" prop="sex">
				<el-radio-group v-model="form.sex">
					<el-radio-button label="man">男</el-radio-button>
					<el-radio-button label="woman">女</el-radio-button>
				</el-radio-group>
			</el-form-item>
			<el-form-item label="生日" prop="birth">
				<el-date-picker v-model="form.birth" type="date" value-format="YYYY-MM-DD" placeholder="选择生日"></el-date-picker>
			</el-form-item>
			<el-form-item label="手机号码" prop="mobile">
				<el-input v-model="form.mobile" placeholder="请输入手机号码" clearable></el-input>
			</el-form-item>
			<el-form-item label="邮箱" prop="email">
				<el-input v-model="form.email" placeholder="请输入邮箱" clearable></el-input>
			</el-form-item>
			<el-form-item label="用户类型" prop="type">
				<el-radio-group v-model="form.type">
					<el-radio-button :label="-1">体验账户</el-radio-button>
					<el-radio-button :label="0">普通用户</el-radio-button>
					<el-radio-button :label="1">分公司管理员</el-radio-button>
					<el-radio-button :label="2">租户管理员</el-radio-button>
					<el-radio-button :label="3">超级管理员</el-radio-button>
				</el-radio-group>
			</el-form-item>
			<el-form-item label="所属租户" prop="tenantObj">
        <yh-table-select v-model="form.tenantObj" :apiObj="$API.system.tenant.list" :props="{label: 'name',value: 'id'}" @change="tenantSelect">
          <el-table-column prop="name" label="租户名称"></el-table-column>
        </yh-table-select>
			</el-form-item>
      <el-form-item label="所属部门" prop="organizationId">
        <el-cascader v-model="form.organizationId"  :show-all-levels="false"  :options="organizations" :props="organizationsProps" clearable></el-cascader>
      </el-form-item>
			<el-form-item label="直接上级" prop="superiorObj">
        <yh-table-select v-model="form.superiorObj" :apiObj="$API.system.user.list" :params="{tenantId: form.tenantObj.id || '-2'}" :props="{label: 'username',value: 'id'}" >
          <el-table-column prop="username" label="用户名"></el-table-column>
          <el-table-column prop="nickName" label="昵称"></el-table-column>
        </yh-table-select>
			</el-form-item>
			<el-form-item label="所属角色" prop="roleIds">
        <sc-select  placeholder="请选择角色" multiple v-model="form.roleIds" :apiObj="$API.system.role.getSelectItem" clearable filterable style="width: 100%;"></sc-select>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaving" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增用户',
					edit: '编辑用户',
					show: '查看'
				},
				visible: false,
				isSaving: false,
				//表单数据
				form: {
					id:"",
					username: "",
					nickName:"",
					roleIds: "",
					organizationId:"",
					sex:"man",
					mobile:"",
					email:"",
					birth:"",
					type:0,
					status:0,
          tenantObj: {},
          superiorObj: {},
				},
				//验证规则
				rules: {
					username: [
						{required: true, message: '请输入登录账号'}
					],
					nickName: [
						{required: true, message: '请输入昵称'}
					],
					type: [
						{required: true, message: '请选择用户类型', trigger: 'change'}
					],
					password: [
						{required: true, message: '请输入登录密码'},
					],
					roleIds: [
						{required: true, message: '请选择所属角色'}
					],
					tenantObj: [
						{required: true, message: '请选择所属租户', trigger: 'change'}
					],
					organizationId: [
						{required: true, message: '请选择所属部门', trigger: 'change'}
					]
				},
				//所需数据选项
				roles: [],
				organizations: [],
				organizationsProps: {
					value: "id",
					label:"title",
          checkStrictly: true,
          expandTrigger:"hover"
				},
				orgParam:{

				}
			}
		},
		watch: {},
		mounted() {
			this.getRole()
			this.getOrganization()
		},
		methods: {
      tenantSelect(){
        this.form.superiorObj = {};
        this.organizations = [];
        this.form.organizationId = "";
        this.getOrganization(this.form.tenantObj.id)
      },
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//加载树数据
			async getRole(){
				var res = await this.$API.system.role.listAll.get();
				this.roles = res.data;
			},
			async getOrganization(tenantId){
				var res = await this.$API.system.organization.list.get({tenantId:tenantId});
				this.organizations = res.data;
			},
			//树点击事件
			orgClick(data){
				this.form.organizationId = data.id
			},
			//表单提交方法
			submit(){
				console.log(JSON.stringify(this.form))
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaving = true;
						var res;
						if(this.mode == 'add')res = await this.$API.system.user.save.post(this.form);
						else if(this.mode == 'edit')res = await this.$API.system.user.edit.post(this.form);
						this.isSaving = false;
						if(res.success){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}
					}else{
						return false;
					}
				})
			},
			//表单注入数据
			setData(data){
				this.form.id = data.id
				this.form.username = data.username
				this.form.nickName = data.nickName
				this.form.sex = data.sex
				this.form.mobile = data.mobile
				this.form.email = data.email
				this.form.birth = data.birth
				this.form.type = data.type
				this.form.roleIds = data.roleIds
        this.$nextTick(() => {
          this.form.tenantObj = {name:data.tenantTitle,id:data.tenantId}
          this.form.superiorObj = {username:data.superiorUsername,id:data.superior}
          this.form.organizationId = [data.organizationId];
        })
        console.log(this.form)
			/*	this.$nextTick(() => {
					this.$refs.orgTree.setData({departmentTitle:data.departmentTitle,id:data.organizationId})
				})*/
			}
		}
	}
</script>

<style>
</style>
